/*
 * Copyright (c) 2009 LastFM.Desktop Anton Pavlik.
 * All Rights Reserved.
 * 
 * You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, 
 * with or without modifications, and in Source or Object form, provided that You meet the following 
 * conditions:
 * 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
 *
 * 2. You must cause any modified files to carry prominent notices stating that You changed the files; 
 * and
 *
 * 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, 
 * patent, trademark, and attribution notices from the Source form of the Work, excluding those notices 
 * that do not pertain to any part of the Derivative Works.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, Licensor provides the Work 
 * (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties 
 * or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. 
 * YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE WORK
 * AND ASSUME ANY RISKS ASSOCIATED WITH YOUR EXERCISE OF PERMISSIONS UNDER THIS LICENSE. 
 */
package org.shake.lastfm.ui.playlist;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.bushe.swing.event.EventBus;
import org.bushe.swing.event.annotation.AnnotationProcessor;
import org.jdesktop.swingx.JXList;
import org.jdesktop.swingx.JXPanel;
import org.shake.lastfm.event.EventTopic;
import org.shake.lastfm.event.TrackSelectedEvent;
import org.shake.lastfm.model.Track;
import org.shake.lastfm.swing.border.RoundCornerBorder;
import org.shake.lastfm.ui.StatusPane;

public class PlaylistPane extends JXPanel
{
    private static final long serialVersionUID = -1527394944309627631L;

    private JXList trackList;
    private TrackListModel trackListModel;

	private TracksHandler tracksHandler;

    public PlaylistPane()
    {
        super();
        buildUI();
    }

    private void buildUI()
    {
        setLayout(new BorderLayout());
        Border emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
        Border border = BorderFactory.createCompoundBorder(new RoundCornerBorder(), emptyBorder);
        JXPanel contentPanel = new JXPanel(new BorderLayout());

        this.trackListModel = createTrackModel();
        this.trackList = new JXList(this.trackListModel);
        this.trackList.setBorder(BorderFactory.createEmptyBorder());
        this.trackList.setBackground(getBackground());

        this.trackList.setCellRenderer(new TrackRenderer());
        this.trackList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.trackList.getSelectionModel().addListSelectionListener(new PlaylistSelectionAdapter());

        JScrollPane sp = new JScrollPane(this.trackList);
        sp.setBorder(BorderFactory.createEmptyBorder());

        JXPanel trackListPanel = new JXPanel(new BorderLayout());
        trackListPanel.setBorder(border);
        trackListPanel.add(sp, BorderLayout.CENTER);

        contentPanel.add(trackListPanel, BorderLayout.CENTER);
        sp.setOpaque(false);
        sp.getViewport().setOpaque(false);

        ProgressPanel progressPanel = new ProgressPanel();
        progressPanel.setBorder(emptyBorder);
        contentPanel.add(progressPanel, BorderLayout.SOUTH);

        UrlPanel urlPanel = new UrlPanel();
        urlPanel.setBorder(emptyBorder);
        contentPanel.add(urlPanel, BorderLayout.NORTH);

        add(contentPanel, BorderLayout.CENTER);

        StatusPane statusPane = new StatusPane();
        add(statusPane, BorderLayout.SOUTH);
    }

    private TrackListModel createTrackModel()
    {
        TrackListModel model = new TrackListModel();
        this.tracksHandler = new TracksHandler(model);
        AnnotationProcessor.process( this.tracksHandler );
        return model;
    }

    private class PlaylistSelectionAdapter implements ListSelectionListener
    {
        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            if (e.getValueIsAdjusting())
            {
                return;
            }

            int selIdx = PlaylistPane.this.trackList.getSelectedIndex();
            Track track = null;
            if (selIdx != -1)
            {
                int realIdx = PlaylistPane.this.trackList.convertIndexToModel(selIdx);
                track = (Track) PlaylistPane.this.trackListModel.getElementAt(realIdx);
            }
            EventBus.publish(EventTopic.TRACK_SELECTED, new TrackSelectedEvent(track));
        }
    }

}